using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Data;
using TexasHoldEmCroupier.Views;
using TexasHoldEmCroupier.Enumerators;
using TexasHoldEmCroupier.UserControls;

namespace TexasHoldEmCroupier.ApplicationController
{
    public partial class Controller
    {
        #region Methods
        private void AssignPotView_Show()
        {
            AssignPotView theView = new AssignPotView();

            theView.buttonOK.Click += 
                new EventHandler(AssignPotView_buttonOK_Click);

            foreach (PlayerStrip thisPlayerStrip in
                this._MainView.panelPlayers.Controls)
            {
                if (!thisPlayerStrip.Action.Equals(Action.Fold))
                {
                    theView.comboboxPlayers.Items.Add(
                        thisPlayerStrip.PlayerName);
                }
            }

            theView.comboboxPlayers.SelectedIndex = 0;

            theView.ShowDialog(this._MainView);
        }

        #region Event Handlers
        private void AssignPotView_buttonOK_Click(
            object sender, 
            EventArgs e)
        {
            Button theOKButton =
                sender as Button;
            AssignPotView theParentView =
                theOKButton.FindForm() as AssignPotView;

            for (int i = 0; i < this._MainView.panelPlayers.Controls.Count; i++)
            {
                PlayerStrip thisPlayerStrip =
                    this._MainView.panelPlayers.Controls[i]
                        as PlayerStrip;

                if (thisPlayerStrip.PlayerName.Equals(
                    theParentView.comboboxPlayers.SelectedItem.ToString()))
                {
                    thisPlayerStrip.Account +=
                        this._MainView.Pot;

                    this.AddCommentaryLine(
                        MessageType.Information,
                        String.Format(
                            CultureInfo.CurrentCulture,
                            "{0} wins the pot of {1:N2}",
                            thisPlayerStrip.PlayerName,
                            this._MainView.Pot));

                    MessageBox.Show(
                        this._MainView,
                        String.Format(
                            CultureInfo.CurrentCulture,
                            "{0} wins the pot of {1:N2}",
                            thisPlayerStrip.PlayerName,
                            this._MainView.Pot),
                        this._MainView.Text,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information,
                        MessageBoxDefaultButton.Button1);
                }
                else if (thisPlayerStrip.Account.Equals(0))
                {
                    this._MainView.panelPlayers.Controls.RemoveAt(i);
                }
            }
            
            this._MainView.Pot = 0;

            theParentView.Hide();
            Application.DoEvents();

            this.StartGame();
        }
        #endregion
        #endregion
    }
}
